<template>
	<div>

	</div>
</template>

<script>
	export default {
		name: 'login',
		data() {
			return {

			}
		},
		created() {

      //判断是否已经是登录状态
      axios.get(this.IP_URL+"/user/auth/info")
        .then(response2 => {
          if (response2.data.msg.code == 0) {
            this.$emit('denglule')
          }else{
            //未登陆
            // js判断是否是微信环境
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == "micromessenger") {
              if (this.$route.query.code) {
                axios.post(this.IP_URL+"/api/wechat/codetoken",{code:this.$route.query.code})
                  .then(response => {
                    if (response.data.msg.code == 0) {

                      localStorage.setItem('openid',response.data.data.openid)
                      localStorage.setItem('userinfo',JSON.stringify(response.data.data.userinfo))

                      if(response.data.data.token){
                        localStorage.setItem('token',response.data.data.token)
                        this.$emit('denglule')
                      }else{
                        this.$emit('denglule')
                      }

                    }else{
                      window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd9e4d5f151960619&redirect_uri=" + encodeURIComponent(location.href) + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
                    }
                  });
              } else {
                window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd9e4d5f151960619&redirect_uri=" + encodeURIComponent(location.href) + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
              }
            }else{
              this.$emit('denglule')
            }
          }
        });
		},
		mounted() {

		},
		methods: {

		}
	}
</script>


<style scoped lang="less">

</style>
